package com.jrx.anytxn.reporting.job.xib.step;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.mapper.ext.ExtFtpInteCoLonMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @comment: 大数据供数(借据维度)
 * @author: liuzb
 * @date: 2021/11/24 15:00
 */
@Service
public class FtpInteCoLonListener implements JobExecutionListener {

    private static Logger logger = LoggerFactory.getLogger(FtpInteCoLonListener.class);

    @Autowired
    private IOrganizationTableService organizationTableService;
    @Resource
    private ExtFtpInteCoLonMapper extFtpInteCoLonMapper;

    @Override
    public void beforeJob(JobExecution jobExecution) {
        //支持重跑
        logger.info("大数据供数(借据维度)FtpInteCoLonListener...");
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
        String todayStr = DateUtils.format(org.getToday(), "yyyy-MM-dd");
        final Date today = DateUtils.parseDate(todayStr, "yyyy-MM-dd");
        logger.info("清空指定日期（{}）的表数据", todayStr);
        // 删除大数据供数(借据维度)-贷款信息
        extFtpInteCoLonMapper.deleteLonInfoCiByBusinessDate(today);
        // 删除大数据供数(借据维度)-还款详细
        extFtpInteCoLonMapper.deleteLonRepayDtlCiByBusinessDate(today);
        // 删除大数据供数(借据维度)-还款计划
        extFtpInteCoLonMapper.deleteLonRepayPlanCiByBusinessDate(today);
        logger.info("表数据指定日期（{}）清理完成", todayStr);
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        logger.info("任务执行结束");
    }
}
